
    PayCtrl.$inject = [
        '$scope',
        '$location',
        '$state',
        '$uibModal',
        'clientService',
        'orderService',
        'deliverInfoService'
    ];
    
    function PayCtrl( $scope, $location, $state, $uibModal, clientSvc, orderSvc, deliverInfoSvc) {
        var vm = this;
        //fake data ($rootScope:merchant,infolist     vm:addrlist,sum)
        $scope.merchant=JSON.parse(sessionStorage.merchant);
        $scope.client=JSON.parse(sessionStorage.client);
        console.log('payctrl client');
        console.log($scope.client);
        $scope.infolist=JSON.parse(sessionStorage.cartInfo);
        
        
        deliverInfoSvc.getDeliverInfoByClient($scope.client._id, function(err, dinfolist) {
            console.log(dinfolist);
            vm.addrlist=dinfolist;
        })
        
        //将session里的infolist提取出food存入bson中
        var flist = [];
        var fcnt = [];
        vm.sum = 0;
        for (var index = 0; index < $scope.infolist.length; index++) {
            vm.sum += ($scope.infolist[index].food.fprice * $scope.infolist[index].count);
            flist.push($scope.infolist[index].food);
            fcnt.push($scope.infolist[index].count);
        }
        console.log("ctrl li deliverinfo");
        
        var neworder = {
            "foods":flist,
            "merchant":$scope.merchant,
            "client":$scope.client,
            "deliverInfo":null,
            "compstate":'0',
            "ostate":'0',
            "otime":new Date(),
            "osum":vm.sum,
            "fcnt":fcnt
        };

        vm.optionval = function(deliverInfo){
                  neworder.deliverInfo=deliverInfo;  
                }

        
        vm.openModal = function() {

            var modalInstace = $uibModal.open({
                template: require('./view/modaltemplate.html'),
                ariaLabelledBy: 'modal-title',
                controller:'addrModalCtrl',
                controllerAs: 'mctrl'
                
            });

            modalInstace.result.then(function (item) {
                vm.addrlist.push(item);
                //模态框消失以后显示所有deliverInfo
                // deliverInfoSvc.getDeliverInfoByClient($scope.client._id, function(err, addrlist) {
                //     vm.addrlist = addrlist;
                // })
            })

        }

        
        vm.pay = function() {
            console.log(neworder);
            orderSvc.addOrder(neworder,function(err, order) {
                vm.order=order;
                sessionStorage.removeItem('cartInfo');
                $location.path('/orderdetail/'+order._id);
            })
        }

        vm.cancel = function() {
            $location.path('/paytocart');
        }

    }

    module.exports = PayCtrl;
